访问电脑版页面

导航:老古开发网手机版STM32单片机STM32单片机的USART

STM32f103 双串口配置和中断

导读://代码仅仅是配置使能串口和相应的中断处理函数,具体功能需要自己定义//IAR 7.1#include "stm32_uart.h"uint8_t card_ture=0;uint16_t add_count=0;uint8_t Key_Data[1]={0};uint8_t Media_Flag=0;uint8_t card_cod
关键字:
stm32f103,中断,双串口配置,

//代码仅仅是配置使能串口和相应的中断处理函数,具体功能需要自己定义

//IAR 7.1

#include "stm32_uart.h"

uint8_t card_ture=0;
uint16_t add_count=0;
uint8_t Key_Data[1]={0};
uint8_t Media_Flag=0;

uint8_t card_code[4]; //[5:1]save the card SN, [0]valid or invalid
uint8_t Temp_Card_Num[4];
uint8_t cardcode_rx_complete = 0;

uint32_t uart_config_record_map[4] = {
0,
UART_CONFIG_UART_MEMORY_ADDR,
FIRMWARE_VERSION_MEMORY_ADDR,
SCHEME_SPECIFIC_MEMORY_ADDR
};

UartConfig_TypeDef the_uart_config;

void stm32_uart_init(uint8_t uart_port, uint32_t BaudRate)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;

/*Fill the structure of uart init*/
if(uart_port == UART2)
{
/*Fill the structure of uart init*/
USART_InitStructure.USART_BaudRate = BaudRate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/*Enable priph clock*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);


/*Config gpio as uart pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/*Config gpio as uart pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/*Init uart*/
USART_Init(USART2, &USART_InitStructure);
/*Enable uart*/
/*Clear flags*/
USART_ClearFlag(USART2, USART_FLAG_TC);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
else if(uart_port == UART1)
{
/*Enable priph clock*/

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

/*Fill the structure of uart init*/
USART_InitStructure.USART_BaudRate = BaudRate;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
if(the_uart_config.checksum==Parity_No)
{
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //USART_WordLength_8b
USART_InitStructure.USART_Parity = USART_Parity_No; //USART_Parity_No
}
else if(the_uart_config.checksum==Parity_Odd)
{
USART_InitStructure.USART_WordLength = USART_WordLength_9b; //USART_WordLength_9b
USART_InitStructure.USART_Parity = USART_Parity_Odd; //USART_Parity_Odd
}
else if(the_uart_config.checksum==Parity_Even)
{
USART_InitStructure.USART_WordLength = USART_WordLength_9b; //USART_WordLength_9b
USART_InitStructure.USART_Parity = USART_Parity_Even; //USART_Parity_Even
}
else
{

}
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

/*Config gpio as uart pin */
GPIO_InitStructure.GPIO_Pin = UART1_TX;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/*Init uart*/
USART_Init(USART1, &USART_InitStructure);
/*Enable uart*/
//USART_Cmd(USART1, ENABLE);
/*Clear flags*/
USART_ClearFlag(USART1, USART_FLAG_TC);
#if 0
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//USART_ITConfig(USART1, USART_IT_RXNE | USART_IT_TXE, ENABLE);
#endif
USART_Cmd(USART1, ENABLE);
}
}


static void stm32_uart_senddatas(uint8_t uart_port, const uint8_t* data, uint16_t len)
{
uint16_t count;
if(uart_port == UART1)
{
for(count = 0; count < len; count++)
{
USART_SendData(USART1, data[count]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
}
else if(uart_port == UART2)
{
for(count = 0; count < len; count++)
{
USART_SendData(USART2, data[count]);
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
}
}
}

static uint8_t* uart1_tx_buffer;
static uint8_t uart1_tx_buffer_len;
static uint8_t uart1_tx_buffer_index = 0;
void stm32_uart_pro_poweron(ProComm_TypeDef comm)
{
USART_ClearFlag(USART1,USART_FLAG_TC); //清除标志位 否则第一位数据丢失
switch(comm)

{

}
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
//USART_Cmd(USART1, ENABLE);
}

void stm32_uart2_senddata(uint8_t uart_port, const uint8_t* data, uint16_t len)
{
USART_ClearFlag(USART2,USART_FLAG_TC); //清除标志位 否则第一位数据丢失
stm32_uart_senddatas(uart_port, data,len);
}

void USART1_IRQHandler(void)
{

if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
USART_ClearITPendingBit(USART1, USART_IT_TXE);
/* Write one byte to the transmit data register */
USART_SendData(USART1, uart1_tx_buffer[uart1_tx_buffer_index++]);

if(uart1_tx_buffer_index == uart1_tx_buffer_len)
{
/* Disable the USARTz Transmit interrupt */
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
uart1_tx_buffer_index = 0;
}
}
}
/*
void USART2_IRQHandler(void)
{
static uint8_t index = 1;
static uint8_t checksum = 0;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
card_code[index] = USART_ReceiveData(USART2);
if(index < 5)
{
checksum ^= card_code[index++];
}
else
{
if(checksum == card_code[5])
{

}
else
{
}
cardcode_rx_complete = 1;
checksum = 0;
}
}
}
*/

void USART2_IRQHandler(void)
{
uint8_t index=0;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
Key_Data[index] = USART_ReceiveData(USART2);
switch(Key_Data[0])
{

}
}
//if(USART_GetITStatus(USART2, USART_IT_TXE) != RESET)
//{
// USART_ClearITPendingBit(USART2, USART_IT_TXE);
//}
}

来源:互联网   作者:karen  2019/1/10 18:20:01
栏目: [ STM32单片机的USART]

相关阅读

STM32 USART串口DMA接收和发送模式

如何采用STM32单片机串口接收数据

STM32F407的UART串口初始化

STM32f103 双串口配置和中断

STM32单片机串口通讯故障排除处理过程

STM32单片机串口的定义及应用方法

STM32F4 USART配置

STM32单片机串口波特率的计算方法解析

基于STM32单片机发送字符串的函数

STM32单片机重映射USART设计

如何利用STM32单片机串口发送字符串

STM32单片机UART发送配置的步骤及方法

如何在STM32串口通信程序中使用printf发送数据

基于STM32单片机的串口使用解析

什么是串口通信?基于STM32的printf打印输出

基于STM32F4单片机USART寄存器控制的设计

基于STM32的串口DMA发送

STM32单片机的Usart2串口的调试方法

基于STM32实现串口的两个分案解析

STM32单片机串口DMA解析